// -----------------------------------------------------------------------
//  <copyright file="RoleResource.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025/1/6 22:46</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Authorization.Properties;
using DaprPlus.Filters;

using OSharp.Localized;

namespace DaprPlus.Authorization.Domain.Entities;

/// <summary>
/// 实体类：角色资源分配
/// </summary>
[LocalizedDescription("String_Auth_RoleResource", typeof(ResAuthorization))]
public class RoleResource : HasIntegrationEventEntityBase, IRoleResource, ICreationTime
{
    /// <summary>
    /// 获取或设置 角色编号
    /// </summary>
    public long RoleId { get; set; }

    /// <summary>
    /// 获取或设置 资源编号
    /// </summary>
    public long ResourceId { get; set; }

    /// <summary>
    /// 获取或设置 资源操作集合JSON字符串
    /// </summary>
    public string? OperationsJson { get; set; }

    /// <summary>
    /// 获取各操作的过滤条件组JSON
    /// Key: 功能操作 (如 "GetPage", "Create")
    /// Value: 数据操作过滤条件 (Key: DataOperation, Value: FilterGroup)
    /// </summary>
    public string? FilterGroupsJson { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedTime { get; set; }

    /// <summary>
    /// 是否锁定当前信息
    /// </summary>
    public bool IsLocked { get; set; }

    /// <summary>
    /// 获取或设置 关联的资源
    /// </summary>
    public virtual Resource Resource { get; set; } = null!;

    /// <summary>
    /// 获取各操作的过滤条件组
    /// Key: 功能操作 (如 "GetPage", "Create")
    /// Value: 数据操作过滤条件 (Key: DataOperation, Value: FilterGroup)
    /// </summary>
    public Dictionary<string, Dictionary<DataOperation, FilterGroup>> GetFilterGroups()
    {
        return FilterGroupsJson.IsNullOrEmpty()
         ? []
         : FilterGroupsJson!.FromTextJson<Dictionary<string, Dictionary<DataOperation, FilterGroup>>>()!;
    }

    /// <summary>
    /// 获取资源操作集合
    /// </summary>
    public string[] GetOperations()
    {
        return OperationsJson.IsNullOrEmpty()
        ? []
        : OperationsJson!.FromTextJson<string[]>()!;
    }
}
